//
//  STBlankView.swift
//  STKitSwift
//
//  Created by air on 2021/1/12.
//

import UIKit
import SnapKit

public enum STBlankType: Int {
    case error // 网络服务错误情况下
    case empty // 获取数据为空的情况下
}

public class STBlankView: UIView {
    // MARK: 1.lift cycle
    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        
        addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        contentView.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.height.equalTo(44)
            make.width.equalTo(80)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        contentView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(button.snp.top).offset(-10)
            make.left.right.equalToSuperview()
        }
        
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.bottom.equalTo(label.snp.top).offset(-10)
            make.left.right.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 2.private methods
    
    // MARK: 3.event response
    @objc private func actionContent(){
        complete?()
    }
    
    private func setType(type: STBlankType){
        switch type {
        case .error:
            imageView.image = UIImage.st_image(named: "icon_blank_error")
            label.text = "当前网络不可用，点击重试"
        default:
            imageView.image = UIImage.st_image(named: "icon_blank_empty")
            label.text = "这里还是空空哒~"
        }
    }
    // MARK: 4.interface
    public var complete: (()->())?
    
    public func showInView(view: UIView, type:STBlankType = .error) {
        setType(type: .error)
        isHidden = false
        if superview == nil{
            view.addSubview(self)
            snp.makeConstraints { (make) in
                make.edges.equalToSuperview()
            }
        }
    }
    
    public func hide(){
        if superview != nil {
            removeFromSuperview()
        }
        isHidden = true
    }
    
    // MARK: 5.getter
    private lazy var contentView: UIControl = {
        let view = UIControl()
        view.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(actionContent)))
        view.backgroundColor = .red
        return view
    }()
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var label: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.lineBreakMode = .byCharWrapping
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = .center
        label.textColor = UIColor(red: 102.0 / 255.0, green: 102.0 / 255.0, blue: 102.0 / 255.0, alpha: 1.0)
        label.isUserInteractionEnabled = true
        return label
    }()
    
    private lazy var button: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 3
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor(red: 108.0 / 255.0, green: 134.0 / 255.0, blue: 234.0 / 255.0, alpha: 1.0).cgColor
        button.layer.borderWidth = 0.5;
        
        button.setTitle("刷新", for: .normal)
        button.setTitleColor(UIColor(red: 86.0 / 255.0, green: 117.0 / 255.0, blue: 236.0 / 255.0, alpha: 1.0), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.addTarget(self, action: #selector(actionContent), for: .touchUpInside)
        return button
    }()
}
